package org.example.content.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.example.base.exception.xuechengPluesException;
import org.example.content.model.dto.EditCourseDto;
import org.example.model.PageParams;
import org.example.model.PageResult;
import org.example.content.mapper.CourseBaseMapper;
import org.example.content.mapper.CourseCategoryMapper;
import org.example.content.mapper.CourseMarketMapper;
import org.example.content.model.dto.AddCourseDto;
import org.example.content.model.dto.CourseBaseInfoDto;
import org.example.content.model.dto.QueryCourseParamsDto;
import org.example.content.model.po.CourseBase;
import org.example.content.model.po.CourseMarket;
import org.example.content.service.CourseBaseInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseBaseInfoServiceImpl implements CourseBaseInfoService {
    @Autowired
    CourseBaseMapper courseBaseMapper;
    @Autowired
    CourseMarketMapper courseMarketMapper;
    @Autowired
    CourseCategoryMapper courseCategoryMapper;

    @Override
    public PageResult<CourseBase> queryCourseBase(PageParams pageParams, QueryCourseParamsDto queryCourseParamsDto) {
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(queryCourseParamsDto.getCourseName()),CourseBase::getName,queryCourseParamsDto.getCourseName());
        queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParamsDto.getAuditStatus()),CourseBase::getAuditStatus,queryCourseParamsDto.getAuditStatus());
     //   queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParamsDto.getPublishStatus()),CourseBase::getAuditStatus,queryCourseParamsDto.getPublishStatus());
        Page<CourseBase> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        Page<CourseBase> pageResult = courseBaseMapper.selectPage(page, queryWrapper);
        List<CourseBase> items = pageResult.getRecords();
        long total = pageResult.getTotal();
        PageResult<CourseBase> courseBasePageResult=new PageResult<CourseBase>(items,total,pageParams.getPageNo(),pageParams.getPageSize());
        return courseBasePageResult;
    }
@Transactional
    @Override
//向两张表添加数据
    public CourseBaseInfoDto AddCourseBase(Long companyId, AddCourseDto addCourseDto) {
        //参数的合法性验证
    CourseBase courseBase = new CourseBase();
    BeanUtils.copyProperties(addCourseDto,courseBase);
    courseBase.setCompanyId(companyId);
    courseBase.setCreateDate(LocalDateTime.now());
    courseBase.setAuditStatus("203001");
   int insert= courseBaseMapper.insert(courseBase);
   if(insert<=0){
       throw new RuntimeException("添加课程失败");
   }
        //第二章表写数据
   CourseMarket courseMarket = new CourseMarket();
   BeanUtils.copyProperties(addCourseDto,courseMarket);
   Long courseId=courseBase.getId();
   courseMarket.setId(courseId);
   saveCourseMarker(courseMarket);
  CourseBaseInfoDto InfoDto= getCourseBaseInfoDto(courseId);
  return InfoDto;
    }
    //查询课程信息
    public CourseBaseInfoDto getCourseBaseInfoDto(Long courseId) {
       CourseBase courseBase= courseBaseMapper.selectById(courseId);
       if(courseBase==null){
           return null;
       }
       CourseMarket courseMarket= courseMarketMapper.selectById(courseId);
        CourseBaseInfoDto courseBaseInfoDto=new CourseBaseInfoDto();
        BeanUtils.copyProperties(courseBase,courseBaseInfoDto);
       //Todo查询课程分类表填充到infoDto
        if(courseMarket!=null){
            BeanUtils.copyProperties(courseMarket,courseBaseInfoDto);
        }
       return courseBaseInfoDto;
    }

    @Override
    public CourseBaseInfoDto updateCourseBase(Long companyId, EditCourseDto editCourseDto) {

        //拿到课程id
        Long courseId = editCourseDto.getId();
        //查询课程信息
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if(courseBase == null){
            xuechengPluesException.cast("课程不存在");
        }

        //数据合法性校验
        //根据具体的业务逻辑去校验
        //本机构只能修改本机构的课程
        if(!companyId.equals(courseBase.getCompanyId())){
            xuechengPluesException.cast("本机构只能修改本机构的课程");
        }

        //封装数据
        BeanUtils.copyProperties(editCourseDto,courseBase);
        //修改时间
        courseBase.setChangeDate(LocalDateTime.now());

        //更新数据库
        int i = courseBaseMapper.updateById(courseBase);
        if(i<=0){
            xuechengPluesException.cast("修改课程失败");
        }
        //更新营销信息
        //todo:更新营销信息
        //查询课程信息
        CourseBaseInfoDto courseBaseInfo = getCourseBaseInfoDto(courseId);

        return courseBaseInfo;
    }

    /* 保存营销信息，存在添加，不存在创造*/
    private int saveCourseMarker(CourseMarket courseMarket) {
        //参数的合法效验
    String charge=courseMarket.getCharge();
    if(StringUtils.isEmpty(charge)){
        throw new RuntimeException("收费课程为空");
    }
    if(charge.equals("201001")){
        if(courseMarket.getPrice()==null||courseMarket.getPrice().floatValue()<=0){
            throw new RuntimeException("价格不能为空，并且大于0");
        }
    }
    //从数据库查询，存在更新，不存在添加
    Long id=courseMarket.getId();
    CourseMarket courseMarket1 =courseMarketMapper.selectById(id);
    if(courseMarket1==null){
       //插入数据库
     int insert=  courseMarketMapper.insert(courseMarket);
     return insert;
    }else {
        //更新数据库
        BeanUtils.copyProperties(courseMarket,courseMarket1);
        courseMarket1.setId(id);
      int i=  courseMarketMapper.updateById(courseMarket1);
      return i;
    }
    }
}
